package com.yiren.dbaa.module.practice_set.line.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.line.contract.LineEditContract;
import com.yiren.dbaa.module.practice_set.line.presenter.LineEditPresenter;
import com.yiren.dbaa.util.KeyBoardUtil;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

/**
 * @author lwb
 * @date : 2022/3/18 9:04
 * @description: 线路保存dialog
 */
public class LineSaveDialog extends Dialog {

    private Activity activity;
    private LineInfoEntity lineInfo;
    /*** 1：新增线路 2：编辑线路 */
    private int pageType;

    private UserParamConfigBean userParamConfig;
    private LineEditContract.Presenter presenter;

    public LineSaveDialog(Activity activity, LineInfoEntity lineInfo, int pageType) {
        super(activity);
        this.activity = activity;
        this.pageType = pageType;
        this.lineInfo = lineInfo;
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
        presenter = new LineEditPresenter(activity);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getContext().setTheme(android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
        setContentView(R.layout.dialog_line_save);
        TextView tvDialogTitle = (TextView) findViewById(R.id.tv_dialog_title);
        RadioGroup rgLineType = (RadioGroup) findViewById(R.id.rg_line_type);
        EditText etLineName = (EditText) findViewById(R.id.et_line_name);
        TextView tvCancel = (TextView) findViewById(R.id.tv_cancel);
        TextView tvSave = (TextView) findViewById(R.id.tv_save);
        etLineName.setInputType(InputType.TYPE_CLASS_TEXT);
        if (pageType == 1) {
            lineInfo.setLineType(1);
        } else {
            if (lineInfo.getLineType() == 1) {
                rgLineType.check(rgLineType.getChildAt(0).getId());
            } else if (lineInfo.getLineType() == 2) {
                rgLineType.check(rgLineType.getChildAt(1).getId());
            }
        }
        if (!userParamConfig.isPartner() && lineInfo.getUserId() != userParamConfig.getUserInfo().getId()) {
            //如果是教练并且修改的不是自己采集的线路，则不能编辑
            for (int i = 0; i < rgLineType.getChildCount(); i++) {
                rgLineType.getChildAt(i).setEnabled(false);
            }
            etLineName.setFocusable(false);
        }
        if (pageType == 1) {
            tvDialogTitle.setText("新建线路");
            etLineName.setHint("请输入新建线路名称");
        } else {
            tvDialogTitle.setText("保存线路");
            etLineName.setHint("线路名称");
            etLineName.setText(lineInfo.getName());
        }
        rgLineType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_exam_line:
                        lineInfo.setLineType(1);
                        break;
                    case R.id.rb_test_line:
                        lineInfo.setLineType(2);
                        break;
                }
            }
        });
        tvCancel.setOnClickListener(v -> dismiss());
        tvSave.setOnClickListener(v -> {
            String lineName = etLineName.getText().toString().trim();
            if (StringUtils.isEmpty(lineName)) {
                MessageBox.show(activity, "线路列表为空！");
                return;
            }
            KeyBoardUtil.hideKeyboard(v);
            LoadDialog.show(activity, "新增线路...", true);
            //判断是新增线路还是修改线路
            if (pageType == 1) {
                lineInfo.setName(userParamConfig.isHasGjdInterface() ? lineName + "(高精度)" : lineName);
            } else {
                if (lineInfo.getName().endsWith("(高精度)")) {
                    lineInfo.setName(lineName.replace("(高精度)", "") + "(高精度)");
                } else {
                    lineInfo.setName(lineName);
                }
            }
            lineInfo.setPointNumber(lineInfo.getLinePoints().size());
            presenter.saveLineAndLinePoint(lineInfo, pageType);
            dismiss();
        });
        // 必须放在加载布局后
        setparams();
    }

    private void setparams() {
        this.setCanceledOnTouchOutside(false);
        WindowManager windowManager = getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        // Dialog宽度
        lp.width = (int) (display.getWidth() * 0.6);
        Window window = getWindow();
        window.setAttributes(lp);
        window.getDecorView().getBackground().setAlpha(0);
    }
}